/*
 * Copyright 2013-2018 Lilinfeng.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nb.protocol.server;

import com.nb.protocol.db.DbCtrl;
import com.nb.protocol.db.PostureDao;
import com.nb.protocol.handle.*;
import com.nb.protocol.service.Service;
import com.nb.protocol.util.NettyConstant;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.log4j.Logger;
import java.math.BigDecimal;

/**
 * @author Lilinfeng
 * @version 1.0
 * @date 2014年3月15日
 */
public class NettyServer {
    private static final Logger logger = Logger.getLogger(NettyServer.class);
    public void bind() throws Exception {

        DbCtrl.init();
        Service.initAndStart();

        // 配置服务端的NIO线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(2);
        try {
            Bootstrap b = new Bootstrap();
            b.group(bossGroup).channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    //.option(ChannelOption.SO_RCVBUF, 65536)
                    //.option(ChannelOption.SO_SNDBUF, 65536)
                    .handler(new LoggingHandler(LogLevel.DEBUG))
                    //.handler(new QuoteServerHandler())
                    .handler(new ChannelInitializer<NioDatagramChannel>() {
                        @Override
                        protected void initChannel(NioDatagramChannel ch) throws Exception {
                            ch.pipeline().addLast(new MessageUDPEncoder());
                            ch.pipeline().addLast(new MessageUDPDecoder());
                            ch.pipeline().addLast(new AcceptorHandler());
                        }
                    });
                   /*.childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast("ProtocolDecoder", new ProtocolDecoder());
                            pipeline.addLast("ProtocolEncoder", new ProtocolEncoder());
                            pipeline.addLast("AcceptorHandler", new AcceptorHandler());


                        }
                    });*/
            // 绑定端口，同步等待成功
            logger.info("Netty server start ok : "
                    + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));
            //b.bind(NettyConstant.REMOTEIP,NettyConstant.PORT).sync().channel().closeFuture().await();
            b.bind(NettyConstant.REMOTEIP,NettyConstant.PORT).sync();

            EventLoopGroup tcpBossGroup = new NioEventLoopGroup(2);
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(tcpBossGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("ProtocolDecoder", new ProtocolDecoder());
                    pipeline.addLast("ProtocolEncoder", new ProtocolEncoder());
                    //pipeline.addLast("IdleStateHandler", new IdleStateHandler(300, 300, 300));
                    pipeline.addLast("AcceptorHandler", new AcceptorHandler());
                }
            }).option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            bootstrap.bind(NettyConstant.REMOTEIP,NettyConstant.PORT).sync();


            // 等待服务端监听端口关闭


        } finally {
            // 优雅退出，释放线程池资源
            //bossGroup.shutdownGracefully();
            //workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyServer().bind();
        int i =  (byte)0x81;
        System.out.println(new BigDecimal(12343333).divide(new BigDecimal(10000)).floatValue());

        ;

        byte n = (byte) 0XAA;

        System.out.println(Integer.parseInt("027F5B75",16));
    }
}
